<?php

namespace model\admin;
use model;
class bannerModel extends model
{

    public static function get_banner_info( $banner_id ){

        $sql = "select * from sh_banner where banner_id=" . $banner_id;

        $query = self::$db->query( $sql );

        return $query->row;

    }

    public static function get_banner_image_list( $banner_id ){


        $sql = "select * from sh_banner_image where banner_id=" . $banner_id;

        $query = self::$db->query( $sql );

        return $query->rows;

    }

    public static function get_list($filter = [])
    {

        $sql = "select * from sh_banner";

        $query = self::$db->query( $sql );

        return $query->rows;
    }

    public static function banner_image_remove( $banner_id ){

        $sql = "delete from sh_banner_image where banner_id= " . $banner_id;
        $query = self::$db->query( $sql );
        return $query->rows;

    }
    public static function banner_image_set( $banner_id, $data ){

        $values = '';

        foreach( $data as $val ){

            $values .= "(" ;
            $values .= "'" . $banner_id . "',";
            $values .= "'" . $val['title'] . "',";
            $values .= "'" . $val['link'] . "',";
            $values .= "'" . $val['image'] . "',";
            $values .= "'" . $val['sort_order'] . "'";
            $values .=  "),";
        }

        $values = trim( $values , ',') ;
        $sql = "insert into sh_banner_image (`banner_id`,`title`,`link`,`image`,`sort_order`) values " . $values ;
        $query = self::$db->query( $sql );
        return $query->num_rows;

    }

    public static function edit( $banner_id, $data ){

        $sql = "update sh_banner set `name`= '" . $data['name']
            . "', `status`=  " . $data['status']
            . " where banner_id=" . $banner_id;

        $query = self::$db->query( $sql );

        return $query->num_rows;

    }

    public static function create( $data ){

        $sql = "insert into sh_banner set `name`= '" . $data['name']
            . "', `status`=  " . $data['status'];

        self::$db->query( $sql );

        return self::$db->getLastId();

    }


    public static function remove( $banner_id ){

        $sql = "delete from sh_banner "
            . " where banner_id=" . $banner_id;

        $query = self::$db->query( $sql );

        return $query->num_rows;

    }
    public static function image_list_remove( $banner_id ){

        $sql = "delete from sh_banner_image "
            . " where banner_id=" . $banner_id;

        $query = self::$db->query( $sql );

        return $query->num_rows;

    }

}